//Documentation complete
/**
 * @author Jason Funk
 * @version 2009FE18
 */

package broker;

import problemDomain.*;
import static problemDomain.ErrorBean.throwError;
import static problemDomain.ErrorBean.ErrorCode.*;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * <code>Broker</code> for <code>FoodItemTypeBean</code>
 */
public class FoodItemTypeBroker extends Broker<FoodItemTypeBean> {
	
	/* Singleton Constructor */
	
	private static FoodItemTypeBroker broker;
	
	/**
	 * Default constructor. Calls the openConnection method.
	 * @throws ErrorBean
	 */
	private FoodItemTypeBroker () throws ErrorBean {
		insertSQL =
		updateSQL =
		deleteFoodItemTypeSQL = null;
	}
	
	/**
	 * Retrieves the static FoodItemTypeBroker instance.
	 * @return static FoodItemTypeBroker
	 * @throws ErrorBean
	 */
	public synchronized static FoodItemTypeBroker getBroker() throws ErrorBean {
		if (broker == null)
			broker = new FoodItemTypeBroker();
		return broker;
	}
	
	/* END Singleton Constructor */
	
	
	/* Prepared Statements */
	
	private PreparedStatement insertSQL;
	private PreparedStatement getInsertSQL () throws ErrorBean {
		return prepare(insertSQL, "INSERT INTO food_item_type SET name = ?");
	}
	
	private PreparedStatement updateSQL;
	private PreparedStatement getUpdateSQL () throws ErrorBean {
		return prepare(updateSQL, "UPDATE food_item_type SET name = ? WHERE food_item_type_id = ?");
	}
	
	private PreparedStatement deleteFoodItemTypeSQL;
	private PreparedStatement getDeleteFoodItemTypeSQL () throws ErrorBean {
		return prepare(deleteFoodItemTypeSQL, "CALL sp_delete_food_item_type(?)");
	}
	
	/* END Prepared Statements */
	
	
	/* (Broker) Methods */
	
	@Override
	public synchronized boolean persist(FoodItemTypeBean fitb) throws ErrorBean {		
		fitb.isValid();
		
		int food_item_type_id = fitb.getId();
		String name = fitb.getName();
		
		if(food_item_type_id == 0) {
			try {
				PreparedStatement insertSQL = getInsertSQL();
				insertSQL.setString(1, name);
				insertSQL.execute();
				return true;
			} catch(SQLException e) {
				throwError(
					BROKER_INSERT_ERROR,
					e.getMessage(),
					this);
			}
		}
		else if(food_item_type_id > 0) {
			try {
				PreparedStatement updateSQL = getUpdateSQL();
				updateSQL.setString(1, name);
				updateSQL.setInt(2, food_item_type_id);
				updateSQL.execute();
				return true;
			} catch(SQLException e) {
				throwError(
					BROKER_UPDATE_ERROR,
					e.getMessage(),
					this);
			}
		}		
		return false;
	}

	@Override
	public synchronized boolean remove(FoodItemTypeBean fitb) throws ErrorBean {
		int food_item_type_id = fitb.getId();
		
		try {
			PreparedStatement deleteFoodItemTypeSQL = getDeleteFoodItemTypeSQL();
			deleteFoodItemTypeSQL.setInt(1, food_item_type_id);
			deleteFoodItemTypeSQL.execute();
			return true;
		} catch(SQLException e) {
			throwError(
				BROKER_DELETE_ERROR,
				e.getMessage(),
				this);
			return false;
		}
	}
	
	@Override
	public synchronized List<FoodItemTypeBean> search(FoodItemTypeBean searchBean) throws ErrorBean {
		List<FoodItemTypeBean> search = new ArrayList<FoodItemTypeBean>();
		FoodItemTypeBean fitb;
		
		boolean isFirst = true;
		StringBuilder sql = new StringBuilder();
		sql.append("SELECT * FROM food_item_type");
		
		//Search by Food Item Type name
		String name = searchBean.getName();
		if (!"".equals(name) && name != null) {
			sql.append(appendClause("name LIKE '%" + name + "%'", isFirst));
			isFirst = false;
		}
		
		//Search by Food Item Type id
		int food_item_type_id = searchBean.getId();
		if (food_item_type_id > 0) {
			sql.append(appendClause("food_item_type_id = " + food_item_type_id, isFirst));
			isFirst = false;
		}
		
		sql.append(';');
		
		try	{
			Statement stmt = conn.createStatement();
			if (!sql.toString().equals("")) {
				ResultSet results = stmt.executeQuery(sql.toString());
				
				while (results.next()) {
					fitb = new FoodItemTypeBean();
					fitb.setId(results.getInt("food_item_type_id"));
					fitb.setName(results.getString("name"));
					
					search.add(fitb);
				}
			}
		} catch (SQLException e) {
			throwError(
				BROKER_SELECT_ERROR,
				e.getMessage(),
				this);
		}
		return search;
	}
	
	@Override
	public synchronized List<FoodItemTypeBean> listAll() throws ErrorBean {
		List<FoodItemTypeBean> search = new ArrayList<FoodItemTypeBean>();
		FoodItemTypeBean fitb;
		
		StringBuilder sql = new StringBuilder("SELECT * FROM food_item_type;");
				
		try	{
			Statement stmt = conn.createStatement();
			if (!sql.toString().equals("")) {
				ResultSet results = stmt.executeQuery(sql.toString());
				
				while (results.next()) {
					fitb = new FoodItemTypeBean();
					fitb.setId(results.getInt("food_item_type_id"));
					fitb.setName(results.getString("name"));
					
					search.add(fitb);
				}
			}
		} catch (SQLException e) {
			throwError(
				BROKER_SELECT_ERROR,
				e.getMessage(),
				this);
		}
		return search;
	}
	
	/* END (Broker) Methods */
	
}
